package com.example.android.fragments

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import androidx.fragment.app.FragmentTransaction
import com.example.android.R
import com.example.android.extensions.AlertBar
import android.widget.TextView
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Observer
import androidx.lifecycle.lifecycleScope
import com.example.android.extensions.AlertBar
import com.example.android.extensions.Services
import com.example.android.models.User
import com.example.android.viewmodels.SharedViewModel
import kotlinx.android.synthetic.main.fragment_self_info2.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
 * <p>
 *
 * </p>
 *
 * @author lxy
 * @since 2022-6-04
 */

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [SelfInfo2Fragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class SelfInfo2Fragment : Fragment() {


    private var btn: Button? = null
    private var tv1:TextView?=null
    private var tv2:TextView?=null
    private var tv3:TextView?=null
    private var tv4:TextView?=null
    private var tv5:TextView?=null
    private var tv6:TextView?=null
    private var tv7:TextView?=null





    private val _Model : SharedViewModel by activityViewModels()


//    val model by lazy {
//        ViewModelProvider(this).get(MyViewModel::class.java)
//    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)


    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)




        //通过ViewModel获取数据
        lifecycleScope.launch(Dispatchers.Main) {
            Services.SharedService.GetSelfInfo(_Model)
        }

        //val model: SelectBedViewModel by viewModels()
        _Model.getSelf().observe(this, Observer<User>{ user ->
            // update UI
            Log.d(toString(), "uuuuuuuuu")
            val name = view.findViewById<TextView>(R.id.name2)
            name.text = user.name
            val sex = view.findViewById<TextView>(R.id.sex2)
            sex.text = user.sex
            val id = view.findViewById<TextView>(R.id.id2)
            id.text = user.identity
            val phone = view.findViewById<TextView>(R.id.phone2)
            phone.text = user.phone
            val number = view.findViewById<TextView>(R.id.number2)
            number.text = user.id.toString()
            val poli_status = view.findViewById<TextView>(R.id.poli_status2)
            poli_status.text = user.political
            val college = view.findViewById<TextView>(R.id.college2)
            college.text = user.college
            val major = view.findViewById<TextView>(R.id.major2)
            major.text = user.major
            val address = view.findViewById<TextView>(R.id.address2)
            address.text = user.address
            val grade = view.findViewById<TextView>(R.id.grade2)
            grade.text = user.classes

        })


        //将修改后的值赋给user
        _Model.getSelf().observe(this, Observer<User>{ user ->
            val name = view.findViewById<TextView>(R.id.name2)
            user.name = name.text.toString()
            val sex = view.findViewById<TextView>(R.id.sex2)
            user.sex = sex.text.toString()
            val grade = view.findViewById<TextView>(R.id.grade2)
            user.classes = grade.text.toString()
            val phone = view.findViewById<TextView>(R.id.phone2)
            user.phone = phone.text.toString()
            val identity = view.findViewById<TextView>(R.id.id2)
            user.identity = identity.text.toString()
            val poli_status = view.findViewById<TextView>(R.id.poli_status2)
            user.political = poli_status.text.toString()
            val college = view.findViewById<TextView>(R.id.college2)
            user.college = college.text.toString()
            val major = view.findViewById<TextView>(R.id.major2)
            user.major = major.text.toString()
            val address = view.findViewById<TextView>(R.id.address2)
            user.address = address.text.toString()

        })

        Log.d(toString(),"fuzhihou"+name2)

        //设置提示灰色字体部分无法修改
        tv1 = view.findViewById(R.id.number2)
        tv1?.setOnClickListener {

//            Toast.makeText(getContext(), "无法修改", Toast.LENGTH_SHORT).show()
            AlertBar {
                it.setTitle("无法修改!")
            }

        }

        tv2 = view.findViewById(R.id.grade2)
        tv2?.setOnClickListener {

//            Toast.makeText(getContext(), "无法修改", Toast.LENGTH_SHORT).show()
            AlertBar {
                it.setTitle("无法修改!")
            }
        }

        tv3 = view.findViewById(R.id.en_year2)
        tv3?.setOnClickListener {

//            Toast.makeText(getContext(), "无法修改", Toast.LENGTH_SHORT).show()
            AlertBar {
                it.setTitle("无法修改!")
            }
        }

        tv4 = view.findViewById(R.id.gra_year2)
        tv4?.setOnClickListener {

//            Toast.makeText(getContext(), "无法修改", Toast.LENGTH_SHORT).show()
            AlertBar {
                it.setTitle("无法修改!")
            }
        }

        tv5 = view.findViewById(R.id.poli_status2)
        tv5?.setOnClickListener {

//            Toast.makeText(getContext(), "无法修改", Toast.LENGTH_SHORT).show()
            AlertBar {
                it.setTitle("无法修改!")
            }
        }

        tv6 = view.findViewById(R.id.college2)
        tv6?.setOnClickListener {

//            Toast.makeText(getContext(), "无法修改", Toast.LENGTH_SHORT).show()
            AlertBar {
                it.setTitle("无法修改!")
            }
        }

        tv7 = view.findViewById(R.id.major2)
        tv7?.setOnClickListener {

//            Toast.makeText(getContext(), "无法修改", Toast.LENGTH_SHORT).show()
            AlertBar {
                it.setTitle("无法修改!")
            }
        }


        btn = view.findViewById(R.id.button_confirm)
        btn?.setOnClickListener(View.OnClickListener() {

            // 建议在提交表单后弹出返回栈, 并提示用户修改成功
            // added by kamanri, 2022/6/7
            requireActivity().supportFragmentManager.popBackStack()
            AlertBar {
                it.setTitle("修改成功!")
                    .setText("个人信息已更新")
//                    .setBackgroundColor(R.color.lime_green)
                    .setBackgroundColor(R.color.crane_purple_900)
            }

            //获取TextView里的值
            _Model.getSelf().postValue(User(
//                view.findViewById<TextView>(R.id.number2).text.toString(),
                null,
                null,
                null,
                view.findViewById<TextView>(R.id.name2).text.toString(),
                view.findViewById<TextView>(R.id.phone2).text.toString(),
                view.findViewById<TextView>(R.id.id2).text.toString(),
                view.findViewById<TextView>(R.id.sex2).text.toString(),
                null,
                null,
                null,
                null,
                view.findViewById<TextView>(R.id.address2).text.toString(),
                view.findViewById<TextView>(R.id.poli_status2).text.toString(),
                view.findViewById<TextView>(R.id.college2).text.toString(),
                view.findViewById<TextView>(R.id.major2).text.toString(),
                view.findViewById<TextView>(R.id.grade2).text.toString(),
                ))


            //将修改后的值赋给user
//            _Model.getSelf().observe(this, Observer<User>{ user ->
//                val name = view.findViewById<TextView>(R.id.name2)
//                user.name = name.text.toString()
//                val sex = view.findViewById<TextView>(R.id.sex2)
//                user.sex = sex.text.toString()
//                val grade = view.findViewById<TextView>(R.id.grade2)
//                user.classes = grade.text.toString()
//                val phone = view.findViewById<TextView>(R.id.phone2)
//                user.phone = phone.text.toString()
//                val identity = view.findViewById<TextView>(R.id.id2)
//                user.identity = identity.text.toString()
//                val poli_status = view.findViewById<TextView>(R.id.poli_status2)
//                user.political = poli_status.text.toString()
//                val college = view.findViewById<TextView>(R.id.college2)
//                user.college = college.text.toString()
//                val major = view.findViewById<TextView>(R.id.major2)
//                user.major = major.text.toString()
//                val address = view.findViewById<TextView>(R.id.address2)
//                user.address = address.text.toString()
//
//            })


        })


    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_self_info2, container, false)


    }

    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment SelfInfo2Fragment.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: Int, param2: String) =
            SelfInfo2Fragment().apply {

            }
    }



}


